package org.unt.logic;

import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;

import org.apache.log4j.Logger;

//import com.canchas.db.DbDao;
//import com.commons.DaoException;

/**
 * @author Rafael
 */
public class PropsUtil {

    private static Logger logger = Logger.getLogger("cupones");

    private static Map<String, String> params = new HashMap<String, String>();

    public static void loadParamsFromDB() {

        try {

            logger.info("About to load params from db");

            params.put("monitor.time", "60000");
            params.put("app.name", "Canchas.Net");
            params.put("mail.from.fantasy", "Admin@Canchas.Net");
            params.put("mail.true.admin", "");
            params.put("mail.smtp.host", "");
            params.put("mail.smtp.auth", "");
            params.put("mail.debug", "");
            params.put("mail.smtp.starttls.enable", "");
            params.put("mail.user.login", "");
            params.put("mail.pass.login", "");

            logger.info("Finished loading params from db");

        } catch (Exception e) {
            logger.error("Error loading parameters from db");
            //logger.error(ThrowableUtil.toString(e));
        }
    }

    private static ResourceBundle rb;
    private static String dbParamsPrefix = "prod";

    /**
     * It sets prefix for db params
     * 
     * @param environment
     *            Values: local, test, prod
     */
    public static void init(String environment) {
        logger.info("Prefix (environment) set to " + environment);
        PropsUtil.dbParamsPrefix = environment;
    }

    public static String getProp(String prop) {

        if (!PropsUtil.params.containsKey(prop)) {
            loadParamsFromDB();
        }

        return PropsUtil.params.get(prop);
    }

    public static String getDbProp(String prop) {

        if (PropsUtil.rb == null) {
            PropsUtil.rb = ResourceBundle.getBundle("app");
        }

        return PropsUtil.rb.getString(dbParamsPrefix + "." + prop);
    }
}